//
//  CJBaseView.swift
//  SinaWeibo
//
//  Created by 程建 on 16/9/23.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//定义协议
@objc protocol CJBaseViewDelegate:NSObjectProtocol {

    //定义协议方法
    
    //登入方法
    func userWillLogin()
    
    //注册方法
    @objc optional func userWillReigster()
}


class CJBaseView: UIView {

    //需要声明一个代理对象属性,需要使用weak修饰,防止循环引用
    weak var delegate:CJBaseViewDelegate?
    
    
    //对外提供一个设置信息的方法
    func updateInfo(title:String,imageName:String? = nil) {
    
        tipLabel.text = title
        //判断是否是首页
        if imageName == nil {
            //是首页
            
            //实现动画效果
            startAnimation()
            
        }else{
        //不是首页
        cricleView.image = UIImage(named: imageName!)
        //隐藏遮罩和房子
        iconView.isHidden = true
        backView.isHidden = true
        }
        
    }
    
    
    //自定义可视图,添加子视图,代码创建使用的方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    //解档默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        addSubview(cricleView)
        cricleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
       //房子需要放在遮罩后面
        
        addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(cricleView)
        }
        
        addSubview(iconView)
        iconView.snp.makeConstraints { (make) in
            make.center.equalTo(cricleView)
            
        }
        
        
        addSubview(tipLabel)
        tipLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(cricleView.snp.bottom).offset(16)
            make.centerX.equalTo(self)
            
        }
        
        addSubview(loginBtn)
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.left.equalTo(tipLabel.snp.left)
            make.width.equalTo(100)
        }
        
        addSubview(registerBtn)
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel.snp.right)
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        let value = CGFloat(237/255.0)
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //登入按钮的方法
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: UIControlEvents.touchUpInside)
        
        //注册按钮的方法
        registerBtn.addTarget(self, action: #selector(registerBtnDicClick), for: UIControlEvents.touchUpInside)
        
    }
     //登入按钮的方法
    @objc private func loginBtnDidClick() {
        
        //使用代理对象调用 协议方法
        //判断代理对象是否为nil 是否响应协议方法
        delegate?.userWillLogin()
    
    }
    
   @objc private func registerBtnDicClick() {
        //可选的方法可以实现,也可不实现
        //必选的方法必须实现
        delegate?.userWillReigster?()
    }
    
    
    
    //懒加载子视图
    //圈圈
    private lazy var cricleView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    //房子
   private lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    //遮罩
    private lazy var backView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))

    //提示文字(闭包懒加载)
    private lazy var tipLabel: UILabel = {
        let l = UILabel(title: "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈", textColor: UIColor.darkGray, fontSize: 14)
        //自动换行
        l.numberOfLines = 0
        //设置最大布局宽度
        l.preferredMaxLayoutWidth = 224
        
        //不要忘记最后要fit,自适应大小
        //设置文字居中
        l.textAlignment = .center
        
        return l
    }()
    
    //注册按钮
    private lazy var registerBtn :UIButton = {
        let btn = UIButton(title: "注册", textColor: UIColor.red, fontSize: 14)
        
        //设置背景颜色
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: UIControlState.normal)
        
        return btn
    }()
    
    //登入按钮
    private lazy var loginBtn: UIButton = {
        
        let btn = UIButton(title: "登入", textColor: UIColor.darkGray, fontSize: 14)
        
        //设置背景颜色
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: UIControlState.normal)
        
        return btn
    }()
    
    private func startAnimation() {
        //使用基础动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画相关参数
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        //设置一个toValue 一圈
        animation.toValue = M_PI * 2
        //设置动画时间
        animation.duration = 15
        //isRemovedOnCompletion 当动画结束的时候或者当页面处于非活动状态的时候移除动画  默认是true
        animation.isRemovedOnCompletion = false
        //添加到圈圈图层上
        cricleView.layer.add(animation, forKey: nil)
    }
    
}
